package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.*;
import com.gxa.zhonglong.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.SuppressPropertiesBeanIntrospector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@CrossOrigin
@RestController
@RequestMapping("/InventoryIn")
@Api(value = "商品入库")
public class InventoryController {

    @Autowired
    GoodsTypeService goodsTypeService = null;

    @Autowired
    GoodsService goodsService = null;

    @Autowired
    AuditService auditService = null;

    @Autowired
    InventoryService inventoryService = null;

    @Autowired
    StoreBarterService storeBarterService = null;

    @Autowired
    SupplierService supplierService = null;

    @PostMapping("/getInventoryNoPage")
    @ApiOperation(value ="商品入库(全查询)" )
    public  Map selectByWhereNoPage(@RequestBody Where where){
        where.setStatus(null);
        where.setPage(null);
        List<Map<String, Object>> selectAll = inventoryService.selectByWhereNoPage(where);

        JsonResult jsonResult = new JsonResult();
        jsonResult.put("selectAll",selectAll);
        jsonResult.put("recordCount",selectAll.size());
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/putInventory")
    @ApiOperation(value ="完成入库" )
    public Map insertInventory(@RequestBody InventoryAndAuditVo inventoryAndAuditVo) {

        Inventory inventory = inventoryAndAuditVo.getInventory();
        Audit audit = inventoryAndAuditVo.getAudit();
        inventoryService.insertInventory(inventory);
        auditService.insertAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


   @PostMapping("/getSupplierAndStore")
   @ApiOperation(value = "(点击新增商品入库)查询supplier表和store表的数据和商品信息")
    public Map selectSupplierAndStore(@RequestBody Where where) throws Exception {
        //获取门店信息
       List<String> store = storeBarterService.selectAllStore();
       //获取供应商信息
       List<Supplier> suppliers = supplierService.selectAll();
       List<String> goodsType = goodsTypeService.getNameList();

       //商品的全查询
       List<Map<String, Object>> goodsList = goodsService.selectAll(where);
       Integer recordCount = goodsService.selectAllCount();
       JsonResult jsonResult = new JsonResult();
       jsonResult.put("store",store);
       jsonResult.put("suppliers",suppliers);
       jsonResult.put("goodstype",goodsType);
      jsonResult.put("goodsList",goodsList);
      jsonResult.put("recordCount",recordCount);
       jsonResult.setCode("1");
       return jsonResult.getDataMap();
    }





    @PostMapping("/getCounter")
    @ApiOperation(value = "(选择门店信息后)查询counter(柜台/类库名称)表的数据")
    public Map selectCounter(@RequestParam("barterName")  String barterName) throws Exception {

        List<String> counter = storeBarterService.selectCounter(barterName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(counter);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getInventory")
    @ApiOperation(value = "(点击查询)根据条件进行多表联合查询" +
            "(根据审核状态和更多查询条件都适用的接口)")
    public Map selectByWhere(@RequestBody Where where ){
        System.out.println(where.getDocumentationDate1());
        List<Map<String, Object>> selectByWhere = inventoryService.selectByWhere(where);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("selectByWhere",selectByWhere);
        jsonResult.put("recordCount",selectByWhere.size());
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }



    @PostMapping("/deleteByGoodsNumber")
    @ApiOperation(value = "(点击删除)根据商品编码删除商品记录")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "number",value = "商品编码",required = true,dataType = "Long")
    )
    public Map deleteByGoodsNumber(@RequestParam("number") String number ) throws BusinessException {
        goodsService.deleteByNumber(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/setByReceiptNumber")
    @ApiOperation(value = "(点击完成编辑)根据单据编号更新入库表的记录")
    public Map updateByReceiptNumber(@RequestBody Inventory inventory){
        inventoryService.updateByReceiptNumber(inventory);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getByReceiptNumberInventory")
    @ApiOperation(value = "(点击编辑/查看)根据单据编号查看入库表的记录")
   public Map getByReceiptNumberInventory(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> maps = inventoryService.selectByReceiptNumberInventory(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByReceiptNumberGoods")
    @ApiOperation(value = "(点击审批)根据单据编号查看相关商品记录以及入库单的记录")
    public Map getByReceiptNumberGoods(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> goods = inventoryService.selectByReceiptNumberGoods(receiptNumber);
        List<Map<String, Object>> inventory = inventoryService.selectByReceiptNumberInventory(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("goods",goods);
        jsonResult.put("inventory",inventory);
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteByInventory")
    @ApiOperation(value = "(删除)根据单据编号删除入库表的记录")
    public Map deleteByInventory(@RequestParam("receiptNumber") String receiptNumber){
        inventoryService.deleteByReceiptNumberInventory(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/setAudit")
    @ApiOperation(value = "(点击审批通过/审批驳回)根据单据编号更新审批表的记录")
    public Map updateByAudit(@RequestBody Audit audit) throws BusinessException {

        auditService.updateByReceiptNumberAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }



}

